from rest_framework import serializers
from .models import *

# 用户序列化器
# 用户的序列化器
class UserSer(serializers.ModelSerializer):
    # 仅作序列化输入，不让暴露密码
    password = serializers.CharField(write_only=True,trim_whitespace=True,max_length=50,min_length=2,)
    class Meta:
        model = UserModel
        fields = ["id", "nickname", "password","avatar_url", "mobile", ]
        read_only_fields = ["id", "date_joined"]

# 收货地址的序列化器
class AddressSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()
    user_mobile = serializers.SerializerMethodField()

    def get_user_name(self,obj):
        return obj.user.nickname

    def get_user_mobile(self,obj):
        return obj.user.mobile

    class Meta:
        model = AddressModel
        fields = "__all__"

# 订单的序列化器
# 订单序列化器
class OrderSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()
    shops = serializers.PrimaryKeyRelatedField(queryset=ShopsModel.objects.all())
    shops_name = serializers.SerializerMethodField()
    shops_avatar_url = serializers.SerializerMethodField()
    pay_method_name = serializers.SerializerMethodField()
    pay_state_name = serializers.SerializerMethodField()
    def get_user_name(self, obj):
        return obj.user.nickname
    def get_shops_name(self, obj):
        return obj.shops.name
    def get_shops_avatar_url(self, obj):
        return obj.shops.avatar_url
    def get_send_name(self, obj):
        if not obj.send:
            return None
        return obj.send.nickname
    def get_pay_method_name(self, obj):
        return obj.get_pay_method_display()
    def get_pay_state_name(self, obj):
        return obj.get_pay_state_display()
    class Meta:
        model = OrderModel
        fields = "__all__"


# 评论序列化器
class CommonSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()
    shops = serializers.PrimaryKeyRelatedField(queryset=ShopsModel.objects.all())
    shop_name = serializers.SerializerMethodField()

    def get_user_name(self,obj):
        return obj.user.nickname

    def get_shop_name(self,obj):
        return obj.shops.name

    class Meta:
        model = CommentModel
        fields = "__all__"

# 订单商品序列化器
class OrderGoodsSer(serializers.ModelSerializer):
    order = serializers.PrimaryKeyRelatedField(queryset=OrderModel.objects.all())
    goods = serializers.PrimaryKeyRelatedField(queryset=GoodModel.objects.all())
    goods_name = serializers.SerializerMethodField()
    goods_image = serializers.SerializerMethodField()

    def get_goods_name(self, obj):
        return obj.goods.name
    def get_goods_image(self, obj):
        return obj.goods.image

    class Meta:
        model = OrderGoodsModel
        fields = "__all__"
